package com.sskj.cndragon.ui.activity;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.cndragon.R;
import com.sskj.cndragon.component.DaggerUserDataComponent;
import com.sskj.cndragon.presenter.ChangePwdActivityPresenter;
import com.sskj.cndragon.view.ToolBarLayout;
import com.sskj.common.base.App;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.BaseHttpConfig;
import com.sskj.lib.Constans;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.router.provider.LogoutProvider;
import com.sskj.lib.util.HideUtil;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;


public class ChangePwdActivity extends BaseActivity<ChangePwdActivityPresenter> {
    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.et_pwd_again)
    EditText etPwdAgain;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.tv0)
    TextView tv0;
    @BindView(R.id.tv_mobile)
    TextView tvMobile;
    @BindView(R.id.et_sms_code)
    EditText etSmsCode;
    @BindView(R.id.tv_get_code)
    TextView tvGetCode;
    @BindView(R.id.btn_submit)
    Button btnSubmit;
    @Inject
    UserViewModel userViewModel;

    private UserData userData;

    private String mobile;
    private String type;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_change_pwd;
    }

    @Override
    public ChangePwdActivityPresenter getPresenter() {
        return new ChangePwdActivityPresenter();
    }

    @Override
    protected void initView() {
        super.initView();
        DaggerUserDataComponent.create().inject(this);
        toolbar.setLeftButtonOnClickListener(view -> finish());
        type = getIntent().getStringExtra(Constans.CHANGE_TYPE);
        mobile = getIntent().getStringExtra(Constans.PHONE);
        if (type.equals(Constans.LOGIN_TYPE)) {
            toolbar.setTitle(App.INSTANCE.getString(R.string.xiugaidenglumima));
            tv0.setText(App.INSTANCE.getString(R.string.denglumima));
            etPwd.setHint(App.INSTANCE.getString(R.string.shezhidenglumima));
            etPwdAgain.setHint(App.INSTANCE.getString(R.string.zaicidenglumima));
        } else if (type.equals(Constans.PAY_TYPE)) {
            toolbar.setTitle(App.INSTANCE.getString(R.string.shezhizhifumima));
            tv0.setText(App.INSTANCE.getString(R.string.zhifumima));
            etPwd.setHint(App.INSTANCE.getString(R.string.qingshezhizhifumima));
            etPwdAgain.setHint(App.INSTANCE.getString(R.string.zaicizhifumima));
        } else if (type.equals(Constans.PAY_EDIT_TYPE)) {
            toolbar.setTitle(App.INSTANCE.getString(R.string.xiugaizhifumima));
            tv0.setText(App.INSTANCE.getString(R.string.zhifumima));
            etPwd.setHint(App.INSTANCE.getString(R.string.qingshezhizhifumima));
            etPwdAgain.setHint(App.INSTANCE.getString(R.string.zaicizhifumima));
        }
        tvMobile.setText(HideUtil.getPhoneHide(mobile));

        ClickUtil.click(300, tvGetCode, () -> {
            if (type.equals(Constans.LOGIN_TYPE)) {
                mPresenter.getCode(mobile, BaseHttpConfig.CODE_RESET);
            } else if (type.equals(Constans.PAY_TYPE) || type.equals(Constans.PAY_EDIT_TYPE)) {
                mPresenter.getCode(mobile, BaseHttpConfig.CODE_PAY);
            }
        });

        ClickUtil.click(300, btnSubmit, () -> {
            if (isEmpty(getEtext(etPwd))) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.appQingshurumima));
                return;
            }
            if (!CheckUtil.isPayPwd(getEtext(etPwd))) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.appQingshuruzhengquedemima));
                return;
            }
            if (!getEtext(etPwd).equals(getEtext(etPwdAgain))) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.appQingmimabuyizhi));
                return;
            }
            if (isEmpty(etSmsCode)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.appQingshuruduanxinyanzhengma));
                return;
            }
            if (type.equals(Constans.LOGIN_TYPE)) {
                mPresenter.changeLoginPwd(mobile, getEtext(etSmsCode), getEtext(etPwd));
            } else if (type.equals(Constans.PAY_TYPE) || type.equals(Constans.PAY_EDIT_TYPE)) {
                mPresenter.changePayPwd(mobile, getEtext(etSmsCode), getEtext(etPwd));
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            } else {
                userData = null;
            }
        });
    }

    public static void start(Context context, String type, String mobile) {
        Intent intent = new Intent(context, ChangePwdActivity.class);
        intent.putExtra(Constans.CHANGE_TYPE, type);
        intent.putExtra(Constans.PHONE, mobile);
        context.startActivity(intent);
    }

    public void getCodeSuccess() {
        startTimeDown(tvGetCode);
    }

    public void changeLoginPwdSuccess() {
        LogoutProvider logoutProvider = ARouter.getInstance().navigation(LogoutProvider.class);
        logoutProvider.logout();
        finish();
    }

    public void changPayPwdSuccess() {
        if (userData != null) {
            userData.setChinaPwd(1);
            userViewModel.update(userData);
        }
        finish();
    }
}
